<template>
  <Modal
    v-model:active="GridModalVueActive"
    transfer
    title="网格剖分"
    :width="480"
    :height="600"
    confirm-text="剖分网格"
    @confirm="executeMeshCreate"
    draggable
    hide-mask
  >
    <NativeScroll use-y-bar height="100%" scroll-style="padding: 12px 24px;">
      <Row :gap="[8]">
        <ConfigProvider :props="providedProps">
          <Form :model="formModel">
            <div class="title">
              <div class="title_color"></div>
              <div class="title_content">选择类型</div>
            </div>
            <FormItem>
              <Select v-model:value="value" :options="options"></Select>
            </FormItem>
            <!-- <Divider></Divider> -->
            <div class="title">
              <div class="title_color"></div>
              <div class="title_content">选择器</div>
            </div>
            <FormItem label="类型:">
              <Select :options="typeOptions" v-model:value="value"></Select>
            </FormItem>
            <!-- <Divider></Divider> -->
            <div class="title">
              <div class="title_color"></div>
              <div class="title_content">表面控制</div>
            </div>
            <FormItem label="单元类型:">
              <Select
                :options="meshTypeOptions"
                v-model:value="formModel.surfaceMeshOptions.elemType"
              ></Select>
            </FormItem>
            <!-- <Column :span="6" :offset="2">
              <FormItem>
                <Checkbox
                  v-model="formModel.surfaceMeshOptions.adaptiveCurvature"
                >强制
                </Checkbox>
              </FormItem>
            </Column> -->
            <FormItem label="尺寸类型:">
              <Select
                :options="sizeOptions"
                v-model:value="currentSizeType"
              ></Select>
            </FormItem>
            <Column :span="12">
              <FormItem label="尺寸:">
                <Input
                  v-model:value="
                    formModel.surfaceMeshOptions.meshSizePercentOfBBox
                  "
                ></Input>
              </FormItem>
            </Column>
            <Column :span="12">
              <FormItem label="最小尺寸:">
                <Input
                  v-model:value="formModel.surfaceMeshOptions.meshSizeMin"
                />
              </FormItem>
            </Column>
            <Column :span="12">
              <FormItem label="增长率:">
                <Input
                  v-model:value="formModel.surfaceMeshOptions.growthRatio"
                ></Input>
              </FormItem>
            </Column>
            <Column :span="12">
              <FormItem label="曲率:">
                <Checkbox v-model:checked="adaptiveCurvature"> </Checkbox>
              </FormItem>
            </Column>
            <Column :span="8">
              <FormItem label="快速波前法:">
                <Checkbox disabled></Checkbox>
              </FormItem>
            </Column>
            <Column :span="6">
              <FormItem label="网格协同:">
                <Checkbox v-model:checked="ifShowTolerance"></Checkbox>
              </FormItem>
            </Column>
            <Column :span="10">
              <FormItem label="容差=" :label-width="48">
                <Input
                  v-model:value="formModel.tolerance"
                  :disabled="!ifShowTolerance"
                ></Input>
              </FormItem>
            </Column>
            <div class="title">
              <div class="title_color"></div>
              <div class="title_content">曲线控制</div>
            </div>
            <FormItem label="加密角度:">
              <Input
                v-model:value="formModel.curveMeshOptions.refinementAngle"
              ></Input>
            </FormItem>
            <Column :span="8">
              <FormItem label="亲近:">
                <Checkbox v-model:checked="enableProximity"></Checkbox>
              </FormItem>
            </Column>
            <Column :span="12">
              <FormItem label="对齐:">
                <Checkbox v-model:checked="alignment"></Checkbox>
              </FormItem>
            </Column>
            <FormItem label="最大边长比:">
              <Input
                v-model:value="formModel.curveMeshOptions.maxLengthRatio"
              ></Input>
            </FormItem>
            <Column :span="8">
              <FormItem label="均匀网格剖分:">
                <Checkbox v-model:checked="sizePriority"></Checkbox>
              </FormItem>
            </Column>
            <Column :span="8">
              <FormItem label="映射网格:">
                <Checkbox v-model:checked="mapping"></Checkbox>
              </FormItem>
            </Column>
          </Form>
        </ConfigProvider>
      </Row>
    </NativeScroll>
  </Modal>
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
import {
  Modal,
  Select,
  Divider,
  Form,
  FormItem,
  ConfigProvider,
  Input,
  Checkbox,
  NativeScroll,
  Row,
  Column,
  Toast,
} from "cax-ui";
import { storeToRefs } from "pinia";
import { useCoreStore, _global } from "@/UI/store";
import { TMesh2dParams } from "@/UI/types/TMesh2dParams";
const coreStore = useCoreStore();

let { GridModalVueActive } = storeToRefs(coreStore);
const value = ref<number>(1);

// * 是否设置网格协同容差
const ifShowTolerance = ref<boolean>(false);
/**
 * * 曲线参数相关
 */
// * 是否设置曲线对齐
const alignment = ref<boolean>(false);
function getRealAlignment() {
  return alignment.value ? 2 : 0;
}

// * 是否设置亲近
const enableProximity = ref<boolean>(false);
function getRealProximity() {
  return enableProximity.value ? 1 : 0;
}

// * 映射网格
const mapping = ref<boolean>(false);
function getRealMapping() {
  return mapping.value ? 1 : 0;
}

// * 均匀网格划分
const sizePriority = ref<boolean>(false);
function getRealSizePriority() {
  return sizePriority.value ? 1 : 0;
}

const adaptiveCurvature = ref<boolean>(false);
function getRealAdaptiveCurvature() {
  return adaptiveCurvature.value ? 1 : 0;
}

const providedProps = {
  default: { clearable: true },
  formItem: { required: false },
};
let formModel: TMesh2dParams = {
  meshSize: 4,
  minSize: 0,
  tolerance: 2,
  encryptionAngle: 60,
  curveMeshOptions: {
    alignment: 0, // 设置是否对齐
    enableProximity: 0, // 设置是否亲近
    maxLengthRatio: 0.0, // 设置最大边长比
    refinementAngle: 60.0, // 设置加密角度
    sizePriority: 0, // 设置尺寸优先级
  },
  mesh2currentComp: 0, // 设置是否剖分当前组件
  surfaceMeshOptions: {
    adaptiveCurvature: 0, // 设置是否自适应曲率
    allowInvalidQuad: 0, // 设置是否允许无效四边形
    elemType: "ETri", // 设置单元类型
    enableProximity: 0, // 设置是否亲近
    forceAFT: 0, // 设置是否强制使用FastAFT
    growthRatio: 1.05, // 设置增长率
    maxNumLayer: 1000000, //  设置最大层数
    meshSizePercentOfBBox: 20.0, //相对尺寸
    meshSizeMin: 0,   // 最小尺寸
    sizePriority: 0, // 设置尺寸优先级
  },
};
const options = [
  {
    label: "剖分",
    value: 1,
  },
];
const typeOptions = [
  {
    label: "面",
    value: 1,
  },
];
const meshTypeOptions = [
  {
    label: "三角形",
    value: "ETri",
  },
  {
    label: "四边形",
    value: "QuadTri",
  },
  {
    label: "四边形2",
    value: "QuadTri2",
  },
  {
    label: "混合类型",
    value: "QualAll",
  },
];
const currentSizeType = ref(1);
const sizeOptions = [
  {
    label: "按比例",
    value: 1,
  },
  {
    label: "按绝对尺寸",
    value: 2,
  },
];

function executeMeshCreate() {
  Toast.loading({ content: "网格剖分中", duration: 0 });
  let params: any = {
    curveMeshOptions: {
      alignment: getRealAlignment(), // 设置是否对齐
      enableProximity: getRealProximity(), // 设置是否亲近,
      maxLengthRatio: Number(formModel.curveMeshOptions.maxLengthRatio), // 设置最大边长比
      refinementAngle: Number(formModel.curveMeshOptions.refinementAngle), //设置加密角度
      sizePriority: getRealSizePriority(), // 尺寸优先级
      mapping: getRealMapping(), // 映射网格
    },
    isSubdivision: Number(formModel.mesh2currentComp), // 设置是否剖分当前组件
    surfaceMeshOptions: {
      adaptiveCurvature: getRealAdaptiveCurvature(), // 设置是否自适应曲率
      allowInvalidQuad: Number(formModel.surfaceMeshOptions.allowInvalidQuad), // 设置是否允许无效四边形
      elemType: formModel.surfaceMeshOptions.elemType, // 设置单元类型
      enableProximity: Number(formModel.surfaceMeshOptions.enableProximity), // 设置是否亲近
      forceAFT: Number(formModel.surfaceMeshOptions.forceAFT), // 设置是否强制使用FastAFT
      growthRatio: Number(formModel.surfaceMeshOptions.growthRatio), // 设置增长率
      maxNumLayer: Number(formModel.surfaceMeshOptions.maxNumLayer), // 设置最大层数
      sizePriority: Number(formModel.surfaceMeshOptions.sizePriority), // 设置尺寸优先级
    },
  };
  if (currentSizeType.value === 1) {
    params["surfaceMeshOptions"]["meshSizePercentOfBBox"] = Number(
      formModel.surfaceMeshOptions.meshSizePercentOfBBox
    ); // 相对尺寸
    params["surfaceMeshOptions"]["meshSizeMinPercentOfBBox"] = Number(
      formModel.surfaceMeshOptions.meshSizeMin
    ); // 最小尺寸
  } else {
    params["surfaceMeshOptions"]["meshSize"] = Number(
      formModel.surfaceMeshOptions.meshSizePercentOfBBox
    );
    params["surfaceMeshOptions"]["meshSizeMin"] = Number(
      formModel.surfaceMeshOptions.meshSizeMin
    ); // 最小尺寸
  }
  _global.Engine.mesh.create(params).then(() => {
    console.log("完成了");
    Toast.close();
    coreStore.setElements();
  });
}
</script>

<style scoped lang="less">
:deep(.vxp-form) {
  display: flex;
  align-items: center;
}

.title {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  width: 100%;

  .title_color {
    width: 4px;
    height: 12px;
    background: #339af0;
    margin-right: 8px;
  }

  .title_content {
    opacity: 1;
    font-family: PingFang SC;
    font-size: 14px;
    font-weight: 600;
    line-height: 22px;
    letter-spacing: 0em;
    font-variation-settings: "opsz" auto;
    /* content/主要一级primary */
    /* 样式描述：--vxp-content-color-primary */
    color: #212529;
  }
}
</style>
